<?php
/**
 * Created by JetBrains PhpStorm.
 * User: joedarkangel3
 * Date: 8/27/11
 * Time: 3:12 PM
 * To change this template use File | Settings | File Templates.
 */
 
class food_option_model_test extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->library('unit_test');
        $dsn = 'mysql://root:c3603896@localhost/unit_test_yun';
        $this->load->database($dsn);
        $this->load->model('food_option_model', 'food_option');
        $this->load->library('unit_test');
        $this->unit->use_strict(true);
    }

    public function index()
    {
        // $category_id, $en_name, $ch_name, $sequence, $price

        // get food option test
        $test_category_id = '2';
        $rows = rand(1,8);
        $foods = $this->food_option->get_option($test_category_id, $rows);
        $this->unit->run(count($foods), $rows, "get food option test");

        // create and read test, v-valid, iv_valid, r-read, c-create
        $iv_c_test = $this->food_option->create($test_category_id, "", "", "bbd", "ttd");
        $this->unit->run(count($iv_c_test['user']), 4, "invalid data fail miserably");

        $v_c_test1 = $this->food_option->create($test_category_id, "enname1", "chname1", "11-11", "4");
        $v_c_test1_id = $this->db->insert_id();
        
        $this->unit->run(count($v_c_test1['user']), 0, "valid food option creation query");

        // valid food option with 0 price
        $v_c_test2 = $this->food_option->create($test_category_id, "esaame2", "chnasfe2", "33-33", "0");
        $v_c_test2_id = $this->db->insert_id();
        $this->unit->run(count($v_c_test2['user']), 0, "valid food option with 0 price");

        

        $r_data1 = $this->food_option->read($v_c_test1_id);
        $r_data2 = $this->food_option->read($v_c_test2_id);
        $this->unit->run($r_data1['sequence'], '11-11', 'data 1 reading test');
        $this->unit->run($r_data2['sequence'], '33-33', 'data 2 reading test');



        // update test, above test already taken care food option model's schema validation method, no need to recheck
        $iv_id_update = $this->food_option->update("no_where_to_find_id", "", "", "", "", "ttd");
        $this->unit->run(array_key_exists('update_query', $iv_id_update['system']), true, "invalid id update query test");

        $duplicate_seq_update = $this->food_option->update($v_c_test2_id, $test_category_id, "enname1", "chname1", "11-11", "0");
        $this->unit->run(array_key_exists('update_action', $duplicate_seq_update['user']), true, "duplicate key fail update");

        // valid update with same sequence
        $this->food_option->update($v_c_test2_id, $test_category_id, "enname1", "chname1","33-33","0");
        $updated_food = $this->food_option->read($v_c_test2_id);
        $this->unit->run($updated_food['price'], '0', 'valid update test');

        // swap test
        $same_place_swap = $this->food_option->swap($v_c_test2_id, $v_c_test2_id);
        $this->unit->run(array_key_exists('swap_sql', $same_place_swap['user']), true, "same place swap test");

        $this->food_option->swap($v_c_test1_id, $v_c_test2_id);
        $food_option1 = $this->food_option->read($v_c_test1_id);
        $food_option2 = $this->food_option->read($v_c_test2_id);
        $this->unit->run('33-33', $food_option1['sequence'], "food 1 sequence after swap");
        $this->unit->run('11-11', $food_option2['sequence'], "food 2 sequence after swap");

        // delete test now
        $system_error_delete = $this->food_option->delete("no_where_to_find");
        $this->unit->run(array_key_exists('delete_query', $system_error_delete['system']), true, 'delete data in broken system');
        
        $this->food_option->delete($v_c_test1_id);
        $this->food_option->delete($v_c_test2_id);

        $gone_data1 = $this->food_option->read($v_c_test1_id);
        $gone_data2 = $this->food_option->read($v_c_test2_id);

        $this->unit->run(count($gone_data1), 0, 'data 1 deleted test');
        $this->unit->run(count($gone_data2), 0, 'data 2 deleted test');

        // report now
        $this->_report();
    }

    private function _report()
    {
        if($this->input->is_ajax_request())
        {
            $final_result = $this->unit->result();
            $passed = 0;
            foreach($final_result as $res)
            {
                if($res['Result'] === "Passed")
                {
                    $passed++;
                }
            }
            if($passed == count($final_result))
            {
                $msg = json_encode(array('result' => 'passed'));
                echo $msg;
            }
            else{
                $msg = json_encode(array('result' => 'failed'));
                echo $msg;
            }
        }
        else{
            echo $this->unit->report();
        }
    }

}